import wx

import logging

import pyciv.resources

class Player():
    def __init__(self, color, name):
        self.color = color
        self.name = name
        self.resources = {pyciv.resources.FOOD: 3,
                          pyciv.resources.GOLD: 10,
                          pyciv.resources.INFLUENCE: 0,
                          pyciv.resources.POWER: 0}

    def _add_resources_for_hex(self, resources, hex, engine):
        for resource, value in engine.state.worker[hex].income(engine.state.terrain[hex]).iteritems():
            resources[resource] += value
        for resource, value in engine.state.worker[hex].upkeep().iteritems():
            resources[resource] -= value

    def calculate_income(self, engine):
        income = {pyciv.resources.FOOD: 0,
                  pyciv.resources.GOLD: 0,
                  pyciv.resources.INFLUENCE: 0,
                  pyciv.resources.POWER: 0}
        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                self._add_resources_for_hex(income, hex, engine)
        return income

    def count_status(self, engine):
        myhexes = 0
        otherhexes = 0
        myworkers = 0
        otherworkers = 0
        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                myhexes += 1
                if engine.state.worker[hex] != pyciv.worker.NONE:
                    myworkers += 1
            else:
                if not engine.state.owner[hex].is_default_player():
                    otherhexes += 1
                    if engine.state.worker[hex] != pyciv.worker.NONE:
                        otherworkers += 1
        return (myhexes, otherhexes, myworkers, otherworkers)

    def get_owned_hexes(self, engine):
        my_hexes = []
        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                my_hexes.append(hex)
        return my_hexes

    def before_turn(self, engine):
        self.resources[pyciv.resources.INFLUENCE] = 0
        self.resources[pyciv.resources.POWER] = 0

        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                engine.state.worker[hex].ready_for_action = True
                self._add_resources_for_hex(self.resources, hex, engine)

    def do_turn(self, engine):
        return True

    def after_turn(self, engine):
        pass

    def is_default_player(self):
        return False

class Default(Player):
    def __init__(self):
        Player.__init__(self, wx.WHITE, "Default")

    def do_turn(self, engine):
        # Default player will be run first every turn
        # So if you want something to happen once every turn but not connected to player
        # here is the spot
        return True

    def is_default_player(self):
        return True

class Human(Player):
    def __init__(self, color, name):
        Player.__init__(self, color, name)
    def do_turn(self, engine):
        return False

class Computer(Player):
    def __init__(self, color, name):
        Player.__init__(self, color, name)

    def do_turn(self, engine):
        my_hexes = self.get_owned_hexes(engine)
        actions_performed = 1
        while my_hexes and actions_performed > 0:
            logging.debug("Player: %s, Hexes: %s" % (self.name, str(my_hexes)))
            actions_performed = 0
            waiting_hexes = []
            for my_hex in my_hexes:
                engine.current_hex = my_hex
                actions = []
                for new_action in engine.state.worker[my_hex].generate_actions(my_hex, engine):
                    if new_action.enabled:
                        actions.append(new_action)
                for hex in engine.state.hexes:
                    action = engine.state.worker[my_hex].generate_hex_action(hex, engine)
                    if action.enabled and not action is pyciv.action.NONE:
                        actions.append(action)

                if len(actions) > 0: 
                    income = self.calculate_income(engine)
                    logging.debug("Hex: %s, Resources: %s, Income: %s" % 
                                  (str(my_hex), 
                                   pyciv.resources.short_string(self.resources),
                                   pyciv.resources.short_string(income)))
                    for action in actions:
                        action.evaluate(self, income, engine)

                    actions.sort(key=lambda action: action.value, reverse=True)
                    for action in actions:
                        logging.debug(str(action))
                    if actions[0].value > 0:
                        actions[0].execute(engine)
                        actions_performed += 1
                        logging.debug("Executed: %s" % str(actions[0]))
                    else:
                        waiting_hexes.append(my_hex)
            my_hexes = list(waiting_hexes)

        engine.current_hex = None

        return True

